local Version = 1.0;
local Author = "nbanyan"
local chatframe = ChatFrame1;
local listName = "BadFriend"
local loaded = false;

-- timer code modified from UberFlex AutoCombat System by Ahmed el-Sawalhy
function badFriendGSimpleTimer_OnLoad(this)

	this:RegisterEvent("LOADING_END");

	this:RegisterEvent("RESET_BAD_FRIEND");
	
	this.delayTime = 5;                   -- seconds to wait
	this.remainingTime = this.delayTime;   -- initialize to delay time

end

function badFriendGSimpleTimer_OnEvent(this, event)

	if ( event == "LOADING_END" and UnitName("player") ~= nil ) then
		FGstartup(listName);
		loaded = true;
	end
	if (event == "RESET_BAD_FRIEND") then
	    friendGlobal_addAll(listName); -- add to global list any names that where recently added
		friendGlobal_removeAll(listName); -- remove from global list any names that where recently added
	end
	
end

function badFriendGSimpleTimer_OnUpdate(this, elapsedTime)

	if ( not loaded ) then
		this.remainingTime = this.remainingTime - elapsedTime;  -- count down

		if (( this.remainingTime < 0 ) and UnitName("player") ~= nil ) then  -- check when we run out of time
			FGstartup(listName);
			loaded = true;
		else
			this.remainingTime = this.delayTime; -- if not loaded yet, wait and try again
		end
	end
end

-- initialize the slash commands and register with AddonManager
function badFriendGlobal_OnLoad(frame)
	-- adds an entry to the AddonManager
	if AddonManager then 
		AddonManager.RegisterAddon("BadFriendGlobal", "The same Blacklist for all characters.","Interface/Addons/friendGlobal/badfriend.tga","Social",nil,"/bfg")
	end
	
	-- import the functions to do the work
	dofile("Interface/AddOns/friendGlobal/friendGlobalCore.lua");
	
	-- slash commands
	SLASH_badFriendGlobal1 = "/bfg";
	SlashCmdList["badFriendGlobal"] = function (editbox, msg)
		if msg == "" then
			badFriendGlobal_help();
		end
		if (msg:match("^add")) then
			friendGlobal_Add(listName, msg:match("^add%s+(.*)"));
		elseif (msg:match("^remove")) then
			friendGlobal_Remove(listName, msg:match("^remove%s+(.*)"));
		elseif (msg == "help") then
			badFriendGlobal_help();
		elseif (msg:match("^list")) then
			friendGlobal_Print(listName);
		elseif (msg:match("^update")) then
			friendGlobal_addAll(listName);
		elseif (msg:match("^reset")) then
			FG_reset(listName);
		elseif (msg:match("^sync")) then
			friendGlobal_LoadList(listName);
			friendGlobal_RemoveFriends(listName);
		elseif (msg:match("^match")) then
			-- remove all entries from the local friend list
			FG_clearLocalList(listName);
			-- add entries from the friendGlobal list
			friendGlobal_LoadList(listName);
		elseif (msg:match("^clear")) then
			-- remove all entries from the local friend list
			FG_clearLocalList(listName);
		elseif (msg:match("^who")) then
			whoAmI(listName, msg:gsub("^who",""));
		end
	end
	
	chatframe:AddMessage("badFriendGlobal loaded. Type /bfg for commands.");
end;

if (not badFriendGlobal_List) then
	badFriendGlobal_List = {};
end
if (not badFriendGlobal_List[GetCurrentRealm()]) then
	badFriendGlobal_List[GetCurrentRealm()] = {}
end

-- prints the shash commands (help)
function badFriendGlobal_help()
	FGecho("/bfg add <name> - adds <name> to your global and local blacklist");
	FGecho("/bfg update - adds all names in the blacklist to the badFriendGlobal list");
	FGecho("/bfg remove <name> - removes <name> from your global and local blacklist");
	FGecho("/bfg list - lists the global blacklist names");
	FGecho("/bfg sync - adds names from badFriendGlobal to the local blacklist and deletes any names removed from badFriendGlobal")
	FGecho("/bfg reset - removes all names from the badFriendGlobal list (leaves local blacklist untouched)");
	FGecho("/bfg match - resets local blacklist to match the badFriendGlobal list");
	FGecho("/bfg clear - removes all names in the local blacklist (leaves badFriendGlobal list untouched)");
	FGecho("/bfg who <name> - Whispers the player <name> the name of the character you where playing when you added them to the badFriendGlobal list.");
	FGecho("/bfg help - to show this message");
end
